// we7/pages/index/addNewAddress.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['浙江省', '杭州市', '余杭区'],
    xuan:true,
    nanLogo:"icon-round",
    nvLogo:"icon-roundcheckfill",
    name:"",
    detail:"",
    mobile:"",
    sex:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      xuan:false,
      region: e.detail.value
    })
    // console.log(this.data.region[0], this.data.region[1], this.data.region[2])
  },
  nan:function(){
    this.setData({
      nanLogo: "icon-roundcheckfill",
      nvLogo: "icon-round",
      sex:1
    })
  },
  nv: function () {
    this.setData({
      nanLogo: "icon-round",
      nvLogo: "icon-roundcheckfill",
      sex: 2
    })
  },
  nameHuo:function(e){
    // console.log(e.detail.value)
    
    this.setData({
      name: e.detail.value
    })
  },
  mobileHuo:function(e) {
    // console.log(e.detail.value)

    this.setData({
      mobile: e.detail.value
    })
  },
  detailHuo: function (e) {
    // console.log(e.detail.value)

    this.setData({
      detail: e.detail.value
    })
  },
  baoCun:function(){
    var that = this
    if (that.data.name==""){
      wx.showToast({
        title: "请输入收货人姓名",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (that.data.xuan === true) {
      wx.showToast({
        title: "请输入收货地址",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (that.data.detail == "") {
      wx.showToast({
        title: "请输入详细地址",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (that.data.mobile == "") {
      wx.showToast({
        title: "请输入收货人手机号",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    
    
    const value = wx.getStorageSync('openid');
    
    app.util.request({
      url: 'admin/WxGoodsApi/address',
      data: {
        uid: value,
        name: that.data.name,
        mobile: that.data.mobile,
        sex: that.data.sex,
        province:that.data.region[0] ,
        city: that.data.region[1] ,
        district:that.data.region[2] ,
        address: that.data.detail
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code==0){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateTo({
              url: './XSMSChooseAddressDetail'
            })
          }, 2000)
        }
      },
      fail:function(){
        wx.showToast({
          title: '服务器未响应',
          icon: 'none',
          duration: 2000
        })
      }
  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})